package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 上传表格信息同步到数据库中
     *
     * @param orderSettingList
     */
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        //判断预约设置容器是否为空
        if (orderSettingList != null && orderSettingList.size() > 0) {
            //遍历容器
            for (OrderSetting orderSetting : orderSettingList) {
                //先查询此日期是否已经预约过
                Long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    //如果已经预约，执行更新
                    orderSettingDao.update(orderSetting);
                } else {
                    //如果未预约。执行插入
                    orderSettingDao.save(orderSetting);
                }
            }
        }
    }

    /**
     * 根据日期查询预约设置数据
     *
     * @param date
     * @return
     */
    @Override
    /*public List<Map> getOrderSettingByMonth(String date) {
        //对传递过来的参数进行处理
        String startDate = date + "-01";
        String endDate = date + "-31";
        //创建map集合
        Map map = new HashMap();
        //将参数添加到集合中
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        //将封装好的map集合传递给持久层
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        //对返回的list集合进行处理
        List<Map> orderSettingList = new ArrayList<>();
        //遍历返回的集合
        for (OrderSetting orderSetting : list) {
            //创建map集合，分装符合前端JSON格式的的key-value, { date: 1, number: 120, reservations: 1 },
            Map orderSettingMap = new HashMap<>();
            orderSettingMap.put("date", orderSetting.getOrderDate().getDate());
            orderSettingMap.put("number", orderSetting.getNumber());
            orderSettingMap.put("reservations", orderSetting.getReservations());
            //将map集合添加到list集合
            orderSettingList.add(orderSettingMap);
        }
        return orderSettingList;
    }*/
    public List<Map> getOrderSettingByMonth(String date) {
        return orderSettingDao.getOrderSettingByMonth(date);
    }

    /**
     * 根据指定日期修改可预约人数
     *
     * @param orderSetting
     */
    @Override
    public void editOrderSettingByDate(OrderSetting orderSetting) {
        //先查询此日期是否已经预约过
        Long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            //如果已经预约，执行更新
            orderSettingDao.update(orderSetting);
        } else {
            //如果未预约。执行插入
            orderSettingDao.save(orderSetting);
        }
    }
}
